package com.ctvit.ichart.action;

import java.math.BigDecimal;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import com.ctvit.framework.struts2.ActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import com.ctvit.common.StringTrans;
import com.ctvit.ichart.service.IchartService;
import com.ctvit.framework.web.context.ContextHolder;

public class IchartAction extends ActionSupport {
	private IchartService ichartService = ContextHolder
			.getBean(IchartService.class);

	private HashMap audNatiJson;
	private HashMap chanlSingleJson;
	private HashMap chanlSubjProJson;

	// config
	private int decDigits = 2; // 小数点位数

	public String getChanlSubjPro() {
		String sqlStr = "  SELECT z_procode,z_pgnam,z_play_date,z_begin_time,z_end_time,\n"
				+ "         z_rate, z_share\n"
				+ "    FROM zcdm_t_prog_aud\n"
				+ "    WHERE z_play_date >= '20130906'\n"
				+ "      AND z_play_date <= '20130912'\n"
				+ "      AND z_chanl_cd  = '0001'\n"
				+ "      AND z_subj_cd   =  'N0295'"
				+ "order by z_play_date asc , z_begin_time asc;";
		// 接受sql结果集
		List<HashMap> sqlResultList;
		// 要往回传的 
		// 图表用的  
		chanlSubjProJson = new HashMap();
		List<Object> chartData = new ArrayList<Object>();
		List<Object> chartLegend = new ArrayList<Object>();
		// 图表下面的表格
		// List<Object> tableData = new ArrayList<Object>();

		// 执行sql
		sqlResultList = ichartService.getObjListBySql(sqlStr);

		if (sqlResultList != null && sqlResultList.size() > 0) {
			for (HashMap sqlHashMap : sqlResultList) {
				HashMap<String, String> chartChanlSubjProData = new HashMap<String, String>();
				HashMap<String, String> chartChanlSubjProLegend = new HashMap<String, String>();

				BigDecimal bdl = new BigDecimal(String.valueOf(sqlHashMap
						.get("z_rate")));
				String str_value = StringTrans.bigDecimalToString(bdl,
						decDigits);
				String str_color = "#0d8ecf";

				chartChanlSubjProData.put("name", "");
				chartChanlSubjProData.put("value", str_value);
				chartChanlSubjProData.put("color", str_color);

				String str_legend = String.valueOf(sqlHashMap.get("z_pgnam"));
				chartChanlSubjProLegend.put("name", str_legend);

				chartData.add(chartChanlSubjProData);
				chartLegend.add(chartChanlSubjProLegend);
			}
		}

		chanlSubjProJson.put("chartData", chartData);
		chanlSubjProJson.put("chartLegend", chartLegend);

		return "chanlSubjProJson";
	}

	public String getAudNati() {
		System.out.println("=22222221111=");
		HttpServletRequest request = ServletActionContext.getRequest();
		// 周期所在年份
		String str_periodYear = request.getParameter("z_period_year");
		System.out.println("str_periodYear : " + str_periodYear);
		if (StringUtils.isEmpty(str_periodYear)) {
			// 年份不能为空
			System.out.println("年份不能为空");
			// return "audNatiJson";
		} else {
			str_periodYear = StringTrans.TransactSQLInjection(str_periodYear);
			boolean periodYearIsNmber = StringTrans.isNumber(str_periodYear);
			if (!periodYearIsNmber) {
				// 年份得是数字
				return "audNatiJson";
			}
		}

		// 周期类型
		String str_periodType = request.getParameter("z_period_type");
		System.out.println("str_periodType : " + str_periodType);
		if (StringUtils.isEmpty(str_periodType)) {
			// 周期类型不能为空
			System.out.println("周期类型不能为空");
			// return "audNatiJson";
		} else {
			str_periodType = StringTrans.TransactSQLInjection(str_periodType);
			boolean periodTypeIsNmber = StringTrans.isNumber(str_periodType);
			if (!periodTypeIsNmber) {
				// 周期类型得是数字
				return "audNatiJson";
			}
		}

		// 周期数字
		String str_period_n = request.getParameter("z_period_n");
		System.out.println("str_period : " + str_period_n);
		if (StringUtils.isEmpty(str_period_n)) {
			// 周期数字不能为空
			System.out.println("周期不能为空");
			// return "audNatiJson";
		} else {
			str_period_n = StringTrans.TransactSQLInjection(str_period_n);
			boolean periodIsNmber = StringTrans.isNumber(str_period_n);
			if (!periodIsNmber) {
				// 周期数字得是数字
				return "audNatiJson";
			}
		}

		String sqlStr = "SELECT z_chanl_name, z_market_percent, z_period_year, z_period, z_is_cctv "
				+ "FROM zcdm_t_aud_nati "
				+ "WHERE z_period_year="
				+ str_periodYear
				+ " "
				+ "AND z_period_type='"
				+ str_periodType
				+ "' "
				+ "AND z_period_n="
				+ str_period_n
				+ " "
				+ "ORDER BY z_market_percent DESC;";
		// 接受sql结果集
		List<HashMap> sqlResultList;
		// 要往回传的
		// 图表用的
		audNatiJson = new HashMap();
		List<Object> chartData = new ArrayList<Object>();
		List<Object> chartLegend = new ArrayList<Object>();
		HashMap<String, String> chartPeriod = new HashMap<String, String>(); // 只为了记一下周期的名字
		// 图表下面的表格
		List<Object> tableData = new ArrayList<Object>();

		// 执行sql
		sqlResultList = ichartService.getObjListBySql(sqlStr);

		if (sqlResultList != null && sqlResultList.size() > 0) {
			int z_tabix = 1;
			BigDecimal otherValue = new BigDecimal(0);

			chartPeriod.put("periodStr", String.valueOf(sqlResultList.get(0)
					.get("z_period")));
			for (HashMap sqlHashMap : sqlResultList) {
				// 记周期名字
				// if (z_tabix == 1) {
				// chartPeriod.put("periodStr",
				// String.valueOf(sqlHashMap.get("z_period")));
				// }
				// 转换图表数据 start
				if (z_tabix <= 10) { // 小于10的分别显示出来
					HashMap<String, String> audNationData = new HashMap<String, String>();
					HashMap<String, String> audNationLegend = new HashMap<String, String>();

					BigDecimal bdl = new BigDecimal(String.valueOf(sqlHashMap
							.get("z_market_percent")));
					String str_value = StringTrans.bigDecimalToString(bdl,
							decDigits);
					String str_color = randomRGB();
					String str_name = (String) sqlHashMap.get("z_chanl_name");

					audNationLegend.put("name", str_name);
					audNationLegend.put("value", str_value);
					audNationLegend.put("color", str_color);

					audNationData.put("name", "");
					audNationData.put("value", str_value);
					audNationData.put("color", str_color);

					chartData.add(audNationData);
					chartLegend.add(audNationLegend);
				} else { // 大于10的汇总相加 显示为“其他”
					BigDecimal bd = new BigDecimal(String.valueOf(sqlHashMap
							.get("z_market_percent")));
					otherValue = otherValue.add(bd);
				}
				// 转换图表数据 end

				// 转换table数据 start
				HashMap<String, String> audNationTableData = new HashMap<String, String>();
				String str_index = String.valueOf(z_tabix);
				String str_chanl_name = String.valueOf(sqlHashMap
						.get("z_chanl_name"));
				BigDecimal bdl = new BigDecimal(String.valueOf(sqlHashMap
						.get("z_market_percent")));
				String str_market_percent = StringTrans.bigDecimalToString(bdl,
						decDigits);
				String str_is_cctv = String
						.valueOf(sqlHashMap.get("z_is_cctv"));

				audNationTableData.put("z_index", str_index);
				audNationTableData.put("z_chanl_name", str_chanl_name);
				audNationTableData.put("z_market_percent", str_market_percent);
				audNationTableData.put("z_is_cctv", str_is_cctv);
				tableData.add(audNationTableData);
				// 转换table数据 end
				z_tabix = z_tabix + 1;
			}

			if (z_tabix > 10) { // 10个之后的“其他”频道展现
				HashMap<String, String> otherAudNationData = new HashMap<String, String>();
				HashMap<String, String> otherAudNationLegend = new HashMap<String, String>();

				String str_value = String.valueOf(otherValue);
				String str_color = randomRGB();

				otherAudNationLegend.put("name", "其他");
				otherAudNationLegend.put("value", str_value);
				otherAudNationLegend.put("color", str_color);

				otherAudNationData.put("name", "");
				otherAudNationData.put("value", str_value);
				otherAudNationData.put("color", str_color);

				// chartData.add(otherAudNationData);
				// chartLegend.add(otherAudNationLegend);
			}

			audNatiJson.put("chartData", chartData);
			audNatiJson.put("chartPeriod", chartPeriod);
			audNatiJson.put("chartLegend", chartLegend);
			audNatiJson.put("tableData", tableData);
		}

		return "audNatiJson";
	}

	public String getChanlSingle() {
		HttpServletRequest request = ServletActionContext.getRequest();
		// 周期所在年份
		String str_periodYear = request.getParameter("z_period_year");
		System.out.println("str_periodYear : " + str_periodYear);
		if (StringUtils.isEmpty(str_periodYear)) {
			// 年份不能为空
			System.out.println("年份不能为空");
			// return "audNatiJson";
		} else {
			str_periodYear = StringTrans.TransactSQLInjection(str_periodYear);
			boolean periodYearIsNmber = StringTrans.isNumber(str_periodYear);
			if (!periodYearIsNmber) {
				// 年份得是数字
				return "audNatiJson";
			}
		}

		// 周期类型
		String str_periodType = request.getParameter("z_period_type");
		System.out.println("str_periodType : " + str_periodType);
		if (StringUtils.isEmpty(str_periodType)) {
			// 周期类型不能为空
			System.out.println("周期类型不能为空");
			// return "audNatiJson";
		} else {
			str_periodType = StringTrans.TransactSQLInjection(str_periodType);
			boolean periodTypeIsNmber = StringTrans.isNumber(str_periodType);
			if (!periodTypeIsNmber) {
				// 周期类型得是数字
				return "audNatiJson";
			}
		}

		// 周期数字
		String str_period_n = request.getParameter("z_period_n");
		System.out.println("str_period : " + str_period_n);
		if (StringUtils.isEmpty(str_period_n)) {
			// 周期数字不能为空
			System.out.println("周期不能为空");
			// return "audNatiJson";
		} else {
			str_period_n = StringTrans.TransactSQLInjection(str_period_n);
			boolean periodIsNmber = StringTrans.isNumber(str_period_n);
			if (!periodIsNmber) {
				// 周期数字得是数字
				return "audNatiJson";
			}
		}

		// 频道代码
		String str_chanl_cd = request.getParameter("z_chanl_cd");
		System.out.println("str_chanl_cd : " + str_chanl_cd);
		if (StringUtils.isEmpty(str_chanl_cd)) {
			// 频道代码不能为空
			System.out.println("频道代码为空");
			// return "audNatiJson";
		} else {
			str_chanl_cd = StringTrans.TransactSQLInjection(str_chanl_cd);
			boolean chanlCdIsNmber = StringTrans.isNumber(str_chanl_cd);
			if (!chanlCdIsNmber) {
				// 周期数字得是数字
				return "audNatiJson";
			}
		}

		chanlSingleJson = new HashMap();
		// 单频道视图 获取图表数据和图标图例
		setChanlSingle_chartDataAndLegend(str_periodYear, str_period_n,
				str_periodType, str_chanl_cd);
		// 单频道视图 获取table数据
		setChanlSingle_tableData(str_periodYear, str_period_n, str_periodType,
				str_chanl_cd);

		return "chanlSingleJson";
	}

	/*
	 * 单频道视图 获取图表数据和图标图例
	 */
	private void setChanlSingle_chartDataAndLegend(String periodYear,
			String period_n, String periodType, String chanl_cd) {
		String sqlStr = "SELECT a.z_subj_cd , c.z_subj_name , a.z_share "
				+ "FROM zcdm_t_top_list AS a "
				+ "INNER JOIN zcdm_t_top_aud AS b ON a.z_top_main_id = b.z_top_main_id "
				+ "INNER JOIN zcdm_t_subj AS c ON a.z_subj_cd = c.z_subj_cd "
				+ "WHERE b.z_year = '" + periodYear + "' "
				+ "AND b.z_period_n = '" + period_n + "' "
				+ "AND b.z_entity_type = '3' " + "AND b.z_period_type = '"
				+ periodType + "' " + "AND a.z_chanl_cd = '" + chanl_cd + "' "
				+ "AND a.z_subj_cd NOT LIKE 'Y%' "
				+ "AND a.z_subj_cd NOT LIKE 'D%' " + "ORDER BY a.z_share DESC "
				+ "LIMIT 10;";

		// 接收sql结果集
		List<HashMap> sqlResultList;
		// 要往回传的
		// 图表用的
		List<Object> chartData = new ArrayList<Object>();
		List<Object> chartLegend = new ArrayList<Object>();
		HashMap<String, String> chartPeriod = new HashMap<String, String>(); // 只为了记一下周期的名字

		// 执行sql
		sqlResultList = ichartService.getObjListBySql(sqlStr);
		if (sqlResultList != null && sqlResultList.size() > 0) {
			chartPeriod.put("periodStr", String.valueOf(sqlResultList.get(0)
					.get("z_period")));

			for (HashMap sqlHashMap : sqlResultList) {
				// 转换图表数据 start
				HashMap<String, String> chanlSingleData = new HashMap<String, String>();
				HashMap<String, String> chanlSingleLegend = new HashMap<String, String>();

				BigDecimal bdl = new BigDecimal(String.valueOf(sqlHashMap
						.get("z_share")));
				String str_share = StringTrans.bigDecimalToString(bdl,
						decDigits);
				String str_color = randomRGB();
				String str_subj_name = String.valueOf(sqlHashMap
						.get("z_subj_name"));

				chanlSingleLegend.put("name", str_subj_name);
				chanlSingleLegend.put("value", str_share);
				chanlSingleLegend.put("color", str_color);

				chanlSingleData.put("name", "");
				chanlSingleData.put("value", str_share);
				chanlSingleData.put("color", str_color);

				chartData.add(chanlSingleData);
				chartLegend.add(chanlSingleLegend);
				// 转换图表数据 end
			}
		}

		if (chanlSingleJson == null) {
			chanlSingleJson = new HashMap();
		}
		chanlSingleJson.put("chartData", chartData);
		chanlSingleJson.put("chartLegend", chartLegend);
	}

	/*
	 * 单频道视图 获取table数据
	 */
	private void setChanlSingle_tableData(String periodYear, String period_n,
			String periodType, String chanl_cd) {
		String last_periodYear = periodYear;
		String last_period_n = String.valueOf(Integer.valueOf(period_n) - 1);
		// 本期信息
		String sqlStr = "SELECT  "
				+ " z_subj_name, current_share, last_share, current_rate, last_rate "
				+ "FROM "
				+ "    (SELECT  "
				+ "        a.z_subj_cd, "
				+ "            a.z_rate AS current_rate, "
				+ "            a.z_share AS current_share, "
				+ "            z_place AS current_place "
				+ "    FROM "
				+ "        zcdm_t_top_list AS a "
				+ "    INNER JOIN zcdm_t_top_aud AS b ON a.z_top_main_id = b.z_top_main_id "
				+ "    WHERE " + "        b.z_year = '"
				+ periodYear
				+ "' "
				+ "            AND b.z_period_n = '"
				+ period_n
				+ "' "
				+ "            AND b.z_entity_type = '3' "
				+ "            AND b.z_period_type = '"
				+ periodType
				+ "' "
				+ "            AND a.z_subj_cd NOT LIKE 'Y%' "
				+ "            AND a.z_subj_cd NOT LIKE 'D%' "
				+ "            AND a.z_chanl_cd = '"
				+ chanl_cd
				+ "') AS t_current "
				+ "        INNER JOIN "
				+ "    (SELECT  "
				+ "        a.z_subj_cd, "
				+ "            a.z_rate AS last_rate, "
				+ "            a.z_share AS last_share, "
				+ "            z_place AS last_place "
				+ "    FROM "
				+ "        zcdm_t_top_list AS a "
				+ "    INNER JOIN zcdm_t_top_aud AS b ON a.z_top_main_id = b.z_top_main_id "
				+ "    WHERE "
				+ "        b.z_year = '"
				+ last_periodYear
				+ "' "
				+ "            AND b.z_period_n = '"
				+ last_period_n
				+ "' "
				+ "            AND b.z_entity_type = '3' "
				+ "            AND b.z_period_type = '"
				+ periodType
				+ "' "
				+ "            AND a.z_subj_cd NOT LIKE 'Y%' "
				+ "            AND a.z_subj_cd NOT LIKE 'D%' "
				+ "            AND a.z_chanl_cd = '"
				+ chanl_cd
				+ "') AS t_last ON t_current.z_subj_cd = t_last.z_subj_cd "
				+ "        INNER JOIN "
				+ "    zcdm_t_subj AS c ON t_current.z_subj_cd = c.z_subj_cd "
				+ "ORDER BY current_place ASC;";
		// 接收sql结果集
		List<HashMap> sqlResultList;
		// 要往回传的
		// 表格用的
		List<Object> tableData = new ArrayList<Object>();

		// 执行sql
		sqlResultList = ichartService.getObjListBySql(sqlStr);
		if (sqlResultList != null && sqlResultList.size() > 0) {
			for (HashMap sqlHashMap : sqlResultList) {
				HashMap<String, String> tableRowData = new HashMap<String, String>();

				String str_subj_name = String.valueOf(sqlHashMap
						.get("z_subj_name"));
				BigDecimal bdl;
				bdl = new BigDecimal(String.valueOf(sqlHashMap
						.get("current_share")));
				String str_current_share = StringTrans.bigDecimalToString(bdl,
						decDigits);
				bdl = new BigDecimal(String.valueOf(sqlHashMap
						.get("last_share")));
				String str_last_share = StringTrans.bigDecimalToString(bdl,
						decDigits);

				bdl = new BigDecimal(String.valueOf(sqlHashMap
						.get("current_rate")));
				String str_current_rate = StringTrans.bigDecimalToString(bdl,
						decDigits);
				bdl = new BigDecimal(String
						.valueOf(sqlHashMap.get("last_rate")));
				String str_last_rate = StringTrans.bigDecimalToString(bdl,
						decDigits);

				tableRowData.put("z_subj_name", str_subj_name);
				tableRowData.put("current_share", str_current_share);
				tableRowData.put("last_share", str_last_share);
				tableRowData.put("current_rate", str_current_rate);
				tableRowData.put("last_rate", str_last_rate);

				tableData.add(tableRowData);
			}
		}
		if (chanlSingleJson == null) {
			chanlSingleJson = new HashMap();
		}
		chanlSingleJson.put("tableData", tableData);
	}

	public String test() {
		// System.out.println("getAudNati");

		// HttpServletRequest request = ServletActionContext.getRequest();
		// String qString = request.getParameter("SUPP_PK");
		// qString =
		// "SELECT count(*) as count FROM supp_evaluation WHERE EXISTS  "
		// +
		// "( select * from supp_supplier where SUPPLIER_ID = supp_evaluation.SUPPLIER_ID  "
		// + "  and pk =  " + TransactSQLInjection(qString) + ");";
		// String sqlStr =
		// "SELECT z_chanl_name, z_market_percent, z_period_year, z_period_n "
		// +"FROM ichart.zcdm_t_aud_nati ;";
		// audNatiJson = ichartService.getObjListBySql(sqlStr);
		return "audNatiJson";
	}

	private String randomRGB() {
		String r, g, b, reString;
		Random random = new Random();
		r = Integer.toHexString(random.nextInt(256)).toUpperCase();
		g = Integer.toHexString(random.nextInt(256)).toUpperCase();
		b = Integer.toHexString(random.nextInt(256)).toUpperCase();
		r = r.length() == 1 ? "0" + r : r;
		g = g.length() == 1 ? "0" + g : g;
		b = b.length() == 1 ? "0" + b : b;
		reString = "#" + r + g + b;
		return reString;
	}

	// set get

	public HashMap getAudNatiJson() {
		return audNatiJson;
	}

	public void setAudNatiJson(HashMap audNatiJson) {
		this.audNatiJson = audNatiJson;
	}

	public HashMap getChanlSingleJson() {
		return chanlSingleJson;
	}

	public void setChanlSingleJson(HashMap chanlSingleJson) {
		this.chanlSingleJson = chanlSingleJson;
	}

	public HashMap getChanlSubjProJson() {
		return chanlSubjProJson;
	}

	public void setChanlSubjProJson(HashMap chanlSubjProJson) {
		this.chanlSubjProJson = chanlSubjProJson;
	}

}
